package GeneticAlgorithm;

public class AllOnesGA {
    public static void main(String[] args) {
        // 在遗传算法里面设置种群的初始值，其中包含了种群大小，变异率，交叉率，以及精英个体数目
        GeneticAlgorithm ga = new GeneticAlgorithm(100, 0.001, 0.95, 2);

        // 初始化种群中个体并将个体染色体长度设置为50
        Population population = ga.initPopulation(50);

        // 评估种群的适应度
        ga.evalPopulation(population);

        // 计数器计数进行了多少代
        int generation = 1;

        while (ga.isTerminationConditionMet(population) == false) {
            // 打印当前种群的最优解
            System.out.println("Best solution :" + population.getFittest(0).toString());
            // 对种群进行交叉
            population = ga.crossoverPopulation(population);
            // 对种群进行评估
            population = ga.mutatePopulation(population);
            // 对种群进行评价
            ga.evalPopulation(population);
            // 代数++
            generation++;
        }
            System.out.println("Found solution in " + generation + "generations");
            System.out.println("Best solution :" + population.getFittest(0).toString());

    }
}
